package cn.ismartv.launchertest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

public class HomeActivity extends Activity implements OnClickListener {
    private static final String TAG = "HomeActivity";


    private GridLayout contentView;

    private List<XmlEntity.PosterEntity> mPosterEntityList;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = (GridLayout) LayoutInflater.from(this).inflate(R.layout.main, null);

        setContentView(contentView);

        new IsmartvUrlClient().doRequest("http://sky.tvxio.com/api/tv/homepage/sharp/", new IsmartvUrlClient.CallBack() {
            @Override
            public void onSuccess(String result) {

                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                try {
                    SAXParser saxParser = saxParserFactory.newSAXParser();
                    XMLReader xmlReader = saxParser.getXMLReader();
                    XmlInfoHandler xmlInfoHandler = new XmlInfoHandler();
                    xmlReader.setContentHandler(xmlInfoHandler);
                    InputSource inputSource = new InputSource(new StringReader(result));
                    xmlReader.parse(inputSource);
                    XmlEntity xmlEntity = xmlInfoHandler.getXmlEntity();
                    mPosterEntityList = xmlEntity.getPosterEntityList();
                    createView(xmlEntity.getPosterEntityList());


                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(Exception exception) {

            }
        });
    }


    private void createView(List<XmlEntity.PosterEntity> posterEntities) {
        for (int i = 0; i < posterEntities.size(); i++) {
            LinearLayout itemView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.item_view, null);
            ImageButton imageButton = (ImageButton) itemView.findViewById(R.id.image);
            imageButton.setTag(i);
            imageButton.setOnClickListener(this);
            TextView title = (TextView) itemView.findViewById(R.id.title);
            title.setText(posterEntities.get(i).getTitle());
            Picasso.with(this).load(posterEntities.get(i).getImage()).into(imageButton);
            contentView.addView(itemView);
        }
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        XmlEntity.PosterEntity posterEntity = mPosterEntityList.get(position);

        Intent intent = new Intent();
        intent.setAction(posterEntity.getAction());
        intent.putExtra("position", posterEntity.getPosition());
        intent.putExtra("title", posterEntity.getTitle());
        intent.putExtra("image", posterEntity.getImage());
        intent.putExtra("homepage_template", posterEntity.getHomepage_template());
        intent.putExtra("homepage_url", posterEntity.getHomepage_url());
        intent.putExtra("fromPage", posterEntity.getFromPage());
        intent.putExtra("url", posterEntity.getUrl());
        startActivity(intent);
    }
}
